import time

from appium import webdriver
from selenium.webdriver.common.by import By
from POappium.Base.basePage import basePage
class SearchPage(basePage):
    def __init__(self,driver):
        super().__init__(driver)
    #检测安装
    # def Xt(self):
    #     self.driver.is_app_installed("com.xhs.app")
    #     if self.driver.is_app_installed("com.xhs.app"):
    #         pass
    #     else:
    #         self.driver.install(r"C:\Users\lifeng\Downloads\小红书.apk")
    #打开小红书软件
    def open(self):
        self.click(By.XPATH,"//*[@text='小红书']")
        time.sleep(2)
    #登陆
    def login(self, username, password):
        # 点击登录入口
        self.click(By.XPATH, "(//android.view.ViewGroup)[2]")
        time.sleep(3)
        # 切换到账号密码登录
        self.click(By.XPATH, "(//android.widget.Button)[2]")
        time.sleep(3)
        # 输入用户名和密码
        self.write(By.XPATH, "//android.widget.EditText[1]", text = username)
        time.sleep(2)
        self.write(By.XPATH, "//*[@text='输入密码']", text = password)
        time.sleep(2)
        # 点击登录按钮
        self.click(By.XPATH, "//*[@content-desc='未选中，同意']")
        time.sleep(3)
        self.click(By.XPATH, "//*[@text='登录']")
        time.sleep(3)
        self.close()
        time.sleep(3)

        # 处理可能的弹窗
        #
        # time.sleep(3)
    #发布笔记
    def Fbj(self,title,content):
        time.sleep(15)
        self.click(By.XPATH,'//*[@content-desc="发布"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="从相册选择"]')
        time.sleep(3)
        self.tap(x = 262,y = 262)
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="下一步"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="下一步"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="添加标题"]')
        time.sleep(3)
        self.write(By.XPATH,'//*[@text="添加标题"]',text = title)
        time.sleep(3)
        self.tap(x = 281,y = 521)
        # self.write(By.XPATH,'(//android.view.ViewGroup)[3]',text = content)
        time.sleep(3)
        self.click(By.XPATH,'//android.widget.Button')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #发布评论
    def Fbpl(self,content):
        self.tap(x = 291,y = 486)
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="评论框"]')
        self.write(By.XPATH,'//*[@content-desc="评论框"]',text = content)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="发送"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="返回"]')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #搜索
    def Sq(self,text):
        self.click(By.XPATH,'//android.widget.Button')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="搜索, "]')
        time.sleep(2)
        self.write(By.XPATH,'//*[@text="搜索, "]',text = text)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="搜索"]')
        self.screenshot(name_prefix = 'screenshot')
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="全部删除"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="返回"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="返回"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="返回"]')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #关注
    def Gz(self):
        self.tap(x = 291,y = 486)
        time.sleep(3)
        self.tap(x = 742,y = 109)
        time.sleep(3)
        self.click(By.XPATH, '//*[@content-desc="返回"]')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #购物手机壳
    def Gw(self,sousuo,name,phonenumber,address):
        time.sleep(5)
        self.click(By.XPATH,'//*[@content-desc="我"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="友好商品友好价"]')
        time.sleep(3)
        self.tap(x = 478,y = 91)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="搜索, "]')
        time.sleep(2)
        self.write(By.XPATH,'//*[@text="搜索, "]',text = sousuo)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="搜索"]')
        time.sleep(3)
        self.tap(x = 694,y = 432)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="立即购买"]')
        time.sleep(3)
        #选择型号
        # self.click(By.XPATH,'//*[@text="iPhone 15"]')
        # time.sleep(3)
        # #添加地址
        # self.click(By.XPATH,'//*[@text="添加"]')
        # time.sleep(3)
        # self.click(By.XPATH,'//*[@text="姓名"]')
        # time.sleep(2)
        # self.write(By.XPATH,'//*[@text="姓名"]',text =name )
        # time.sleep(3)
        # self.click(By.XPATH,'//*[@text="手机号"]')
        # time.sleep(2)
        # self.write(By.XPATH,'//*[@text="手机号"]',text = phonenumber)
        # time.sleep(3)
        # self.click(By.XPATH,'//*[@content-desc="省、市、区、街道"]')
        # time.sleep(3)
        # self.click(By.XPATH,'(//android.widget.TextView[@text="北京"])[2]')
        # time.sleep(3)
        # self.click(By.XPATH,'//*[@text="昌平区"]')
        # time.sleep(3)
        # self.swipe(451,1548,451,229,2000)
        # time.sleep(2)
        # self.click(By.XPATH,'//*[@text="暂不选择"]')
        # time.sleep(3)
        # self.click(By.XPATH,'//*[@text="社区、门牌号等"]')
        # self.write(By.XPATH,'//*[@text="社区、门牌号等"]',text = address)
        # time.sleep(2)
        # self.click(By.XPATH,'//*[@text="保存并使用"]')
        # time.sleep(2)
        # self.swipe(451,1548,451,229,2000)
        # time.sleep(2)
        self.tap(778,1549)
        # self.click(By.XPATH,'//*[@text="微信支付"]')
        # time.sleep(30)
        # self.tap(x = 774,y = 1553)
        # #等待跳转输入密码
        time.sleep(30)
        self.close()
        time.sleep(3)
    #添加地址
    def Dz(self):
        time.sleep(5)
        self.click(By.XPATH,'//*[@text="我"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="友好商品友好价"]')
        time.sleep(3)

        # self.swipe(8.0/9.0,11.0/80.0,1.0/9.0,11.0/80.0,2000)
        self.swipe(802,220,100,220,2000)
        # self.swipe(900,1600,60,1600,2000)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="地址管理"]')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #加入购物车
    def Jrgwc(self,content):
        time.sleep(5)
        self.click(By.XPATH,'//*[@text="我"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="友好商品友好价"]')
        time.sleep(3)
        self.tap(x=478, y=91)
        time.sleep(3)
        self.click(By.XPATH, '//*[@text="搜索, "]')
        time.sleep(2)
        self.write(By.XPATH, '//*[@text="搜索, "]', text=content)
        time.sleep(3)
        self.click(By.XPATH, '//*[@text="搜索"]')
        time.sleep(3)
        self.tap(x=694, y=432)
        time.sleep(3)
        self.click(By.XPATH, '//*[@text="加入购物车"]')
        time.sleep(3)
        # self.click(By.XPATH, '//*[@text="iPhone 16"]')
        # time.sleep(3)
        self.click(By.XPATH, '//*[@text="确定"]')
        time.sleep(3)
        self.close()
        time.sleep(3)

    #删除笔记
    def Sj(self):
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="我"]')
        time.sleep(8)
        self.tap(x = 202,y = 1184)
        time.sleep(3)
        self.click(By.XPATH,'//*[@resource-id="com.xingin.xhs:id/moreOperateIV"]')
        time.sleep(3)
        self.swipe(793,1481,99,1481,2000)
        time.sleep(2)
        self.click(By.XPATH,'//*[@text="删除"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="删除笔记"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="确认删除"]')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #发布评论
    def Fbpl(self,content):
        time.sleep(5)
        self.tap(x = 225,y = 481)
        time.sleep(8)
        self.swipe(447,1285,447,900,2000)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="爱评论的人运气都不差"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="爱评论的人运气都不差"]')
        time.sleep(2)
        self.write(By.XPATH,'//*[@text="爱评论的人运气都不差"]',text = content)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="发送"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@content-desc="返回"]')
        time.sleep(3)
        self.close()
        time.sleep(3)
    #编辑我的昵称
    def Ewm(self,content):
        time.sleep(5)
        self.click(By.XPATH,'//*[@content-desc="我"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="编辑资料"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="名字"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@resource-id="text-input"]')
        time.sleep(3)
        self.clean()
        time.sleep(3)
        self.write(By.XPATH,'//*[@resource-id="text-input"]',text = content)
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="确认"]')
        time.sleep(3)
        self.click(By.XPATH,'//*[@text="保存"]')
        time.sleep(3)
        self.tap(589,871)
        time.sleep(3)
        self.close()
        time.sleep(3)

    #关闭
    def guan(self):
        self.close()
